
#include "Thread.h"


#if 1
#include <errno.h>

namespace basterd
{
	// --------------------------------------------------------------------
	// Mutex
	// --------------------------------------------------------------------
	Mutex::Mutex(void)
	{
		pthread_mutex_init ( &_mutex, 0 );
	}
	// --------------------------------------------------------------------
	Mutex::~Mutex(void)
	{
		pthread_mutex_destroy ( &_mutex );
	}
	// --------------------------------------------------------------------
	void Mutex::lock(void)
	{
		pthread_mutex_lock( &_mutex );
	}
	// --------------------------------------------------------------------
	bool Mutex::tryLock(void)
	{
		int rc = pthread_mutex_lock ( &_mutex );
		switch(rc)
		{
		// the only success case
		case 0:
			return true;
		// could not lock (usual case)
		case EBUSY:
			return false;
		// error:
		case EINVAL:
			//BASTERD_WARNF( "Mutex wasn't initialized" );
			std::cout << "[WARNING]" << "Mutex wasn't initialized" << std::endl;
			return false;
		// unknown error:
		default:
		{
			//BASTERD_WARNF( "tryLock() failed. Unknown error: " << rc );
			std::cout << "[WARNING]" << "tryLock() failed. Unknown error: " << std::endl;
			return false;
		}
		}

		return false;
	}
	// --------------------------------------------------------------------
	void Mutex::unlock(void)
	{
		pthread_mutex_unlock ( &_mutex );
	}
	// --------------------------------------------------------------------
}

#endif
